<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>test-sdk</title>
	<script src="../lib/callhelper.min.js"></script>
	<script type="text/javascript">
		window.onload = function (){
	        document.getElementById('init').addEventListener('click', function () {
	            initSoftPhone();
	        }, false);

			document.getElementById('makecall').addEventListener('click', function () {
	            var callnum = document.getElementById('tel_num').value.replace(/\s/g,'');
	            CallHelper.invite(callnum, inviteCallback);
	        }, false);

	        document.getElementById('hangup').addEventListener('click', function () {
	            CallHelper.hangup();
	        }, false);
		}

		/**
		 * 初始化softphone bar
		 */
		function initSoftPhone(){
			var url = prompt("请输入请求地址(携带https://)","");
			var name = prompt("请输入你的用户名","");
			var password = prompt("请输入你的密码","");
			var config = {uname: name, pwd: password, url: url, debug: true, isAutoAnswer: false, stateListenerCallBack: stateCallback, forceAnswerWhenRing: true, autoReady: true};
			CallHelper.init(config, initCallback);
		}

		/**
		 * 设置状态监听回调
		 */
		function stateCallback(data){
			console.info(data);
			if(data.msg === "READY"){

			}else if(data.msg === "RINGING"){
				document.getElementById('callnum').innerHTML = data.data.phoneNum;
			}else if(data.msg === "HANGUP"){
				document.getElementById('calluuid').innerHTML = '';
				document.getElementById('msg').innerHTML = '';
				document.getElementById('callnum').innerHTML = '';
			}
		}

		/**
		 * 初始化方法回调是否成功
		 */
		function initCallback(data){
			console.info(data);
			if(data.successChange){
				//显示本机号码
				document.getElementById('agentnum').innerHTML = data.data.agentnumber;
				//电话条ready状态变更
				//CallHelper.ready();
				alert('您已登录成功！');
			}else{
				alert('登录失败，请联系管理员！');
			}
		}

		/**
		 * makecall 外呼回调
		 */
		function inviteCallback(data){
			console.info('callmake callback data is :'+ data.state + "   msg :" + data.msg);
			if(data.state === 1){
				showUUID(data.msg);
			}else{
				document.getElementById('msg').innerHTML = data.msg;
			}
		}

		/**
		 * 显示电话uuid
		 */
		function showUUID(uuid){
			document.getElementById('calluuid').innerHTML = uuid;
		}
	</script>
</head>
<body>
<h2>第一步:</h2>
<button id="init">初始化</button>
<h3>坐席号码:  <span id='agentnum'></span></h3>

<h2>第二步:</h2>
<input type="text" id="tel_num" name="phoneNum"/>
<button id="makecall">外拨</button>
<button id="hangup">挂机</button> <br/>

<h2>电话数据:</h2>
<h3>电话号码:  <span id='callnum'></span></h3>
<h3>电话uuid:  <span id='calluuid'></span></h3>
<h3 id=""><p id='msg'></p></h3>

</body>
</html>